﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UILoopItem : MonoBehaviour {
    private object _dataobj;
    private int _idx;
    private GameObject _item;
    // Use this for initialization
    void Start () {
        
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void UpdateItem(int index, GameObject item)
    {
        _idx = index;
        _item = item;
        if(_item.GetComponent<Button>()==null)
        {
            _item.gameObject.AddComponent<Button>().onClick.AddListener(ClickItem);
        }
        showItem();
    }

    public int GetIdx()
    {
        return _idx;
    }

    public void Data(object obj)
    {
        _dataobj = obj;
    }

    public void UpdateItem(int idx, IList obj)
    {
        if(_idx!= idx)
        {
            _idx = idx;
        }
        if(obj.Count>_idx && _dataobj!= obj[_idx])
        {
            _dataobj = obj[_idx];
            showItem();
        }
    }

    private void ClickItem()
    {
        //Debug.Log("我点击的是哪个条:"+ _idx);
        //List<int> asdf = new List<int>();
        //for (int m = 0; m < 50; m++)
        //{
        //    asdf.Add(m*2);
        //}
        //GameObject.Find("Canvas/Scroll View").GetComponent<ListUI>().Data(asdf);
        GameObject.Find("Canvas/Scroll View").GetComponent<ListUI>().MoveToItem(20);
    }

    private void showItem()
    {
        _item.transform.GetChild(0).GetComponent<Text>().text = "" + _dataobj;
    }
}
